package com.ctgu.rabbitmq.service.impl;

import com.ctgu.rabbitmq.rabbitmq.queue.maxLength.MaxLengthConfig;
import com.ctgu.rabbitmq.rabbitmq.queue.messageExpired.MessageExpiredConfig;
import com.ctgu.rabbitmq.rabbitmq.queue.queueExpired.QueueExpiredConfig;
import com.ctgu.rabbitmq.rabbitmq.queue.reject.RejectConfig;
import com.ctgu.rabbitmq.service.QueueService;
import com.ctgu.rabbitmq.utils.ApiResult;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

/**
 * @ ClassName QueueServiceImpl
 * @ Author Alex2
 * @ Date 2025/8/15 9:12
 **/
@Log4j2
@Service
public class QueueServiceImpl implements QueueService {

    private final RabbitTemplate rabbitTemplate;

    private static final String TAG = "Alex";

    public QueueServiceImpl(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }

    /**
     * @ Author: Alex
     * @ Description: 单条消息过期
     */
    @Override
    public ApiResult messageExpired() {
        String msg = "Hello World";
        MessageProperties props = new MessageProperties();
        props.setExpiration(String.valueOf(5000)); // 单条消息过期时间，单位毫秒
        Message message = new Message(msg.getBytes(), props);
        rabbitTemplate.send(
                MessageExpiredConfig.NORMAL_EXCHANGE_MESSAGE_EXPIRED,
                MessageExpiredConfig.NORMAL_ROUTING_KEY_MESSAGE_EXPIRED,
                message);
        log.debug("{} : 发送消息成功 : {}", TAG, message);
        return ApiResult.success("发送消息成功", message);
    }

    /**
     * @ Author: Alex
     * @ Description: 队列过期
     */
    @Override
    public ApiResult queueExpired() throws InterruptedException {
        String message = "Hello Queue TTL";
        rabbitTemplate.convertAndSend(
                QueueExpiredConfig.NORMAL_EXCHANGE_QUEUE_EXPIRED,
                QueueExpiredConfig.NORMAL_ROUTING_KEY_QUEUE_EXPIRED,
                message
        );
        log.debug("{} : 发送消息成功 : {}", TAG, message);
        return ApiResult.success("发送消息成功", message);
    }

    /**
     * @ Author: Alex
     * @ Description: 队列达到最大长度
     */
    @Override
    public ApiResult maxLength() {
        for (int i = 1; i <= 10; i++) {
            String msg = "消息 " + i;
            rabbitTemplate.convertAndSend(
                    MaxLengthConfig.NORMAL_EXCHANGE_MAX,
                    MaxLengthConfig.NORMAL_ROUTING_KEY_MAX,
                    msg
            );
            log.debug("{} : 发送消息 : {}", TAG, msg);
        }
        log.debug("{} : 发送消息成功，共发送10次", TAG);
        return ApiResult.success("发送消息成功，共发送10次");
    }

    /**
     * @ Author: Alex
     * @ Description: 消费者拒绝消费且不重新投递
     */
    @Override
    public ApiResult reject() {
        String msg = "Hello World";
        Message messageHello = new Message(msg.getBytes());
        rabbitTemplate.convertAndSend(
                RejectConfig.NORMAL_EXCHANGE_REJECT,
                RejectConfig.NORMAL_ROUTING_KEY_REJECT,
                messageHello,
                message -> {
                    message.getMessageProperties().setExpiration(String.valueOf(5000));
                    return message;
                }
        );
        log.debug("{} : 发送消息成功 : {}", TAG, messageHello);
        return ApiResult.success("发送消息成功", messageHello);
    }
}